package com.qinggeng.project.system.service.impl;

import java.util.List;

import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.enums.UserTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.JobUser;
import com.qinggeng.project.system.domain.SysUser;
import com.qinggeng.project.system.mapper.JobUserMapper;
import com.qinggeng.project.system.service.IJobUserService;
import com.qinggeng.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

/**
 * 求职用户Service业务层处理
 *
 * @author smallfish
 * @date 2020-12-23
 */
@Service
public class JobUserServiceImpl implements IJobUserService
{
    @Resource
    private JobUserMapper jobUserMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询求职用户
     *
     * @param jobUserId 求职用户ID
     * @return 求职用户
     */
    @Override
    public JobUser getJobUserById(Long jobUserId)
    {
        return jobUserMapper.getJobUserById(jobUserId);
    }

    public JobUser getJobUserByUserId(Long userId){
        JobUser jobUser=jobUserMapper.getJobUserByUserId(userId);
        if(jobUser!=null){
            return jobUser;
        }
        SysUser sysUser=sysUserService.selectUserById(userId);
        if(null==sysUser){
            throw new CustomException("用户不存在");
        }
        return new JobUser(sysUser);
    }

    public Long getJobUserId(){

        return getJobUser().getJobUserId();
    }

    public JobUser getJobUser(){
        JobUser jobUser=jobUserMapper.getJobUserByUserId(ApiUtils.getUserId());
        if(null==jobUser||null==jobUser.getJobUserId()){
            throw new CustomException("请先完善个人信息", ShowTypeEnum.用户.getValue());
        }
        return jobUser;
    }

    /**
     * 查询求职用户列表
     *
     * @param resumeUser 求职用户
     * @return 求职用户
     */
    @Override
    public List<JobUser> listJobUser(JobUser resumeUser)
    {
        return jobUserMapper.listJobUser(resumeUser);
    }

    /**
     * 新增求职用户
     *
     * @param jobUser 求职用户
     * @return 结果
     */
    @Override
    public boolean insertJobUser(JobUser jobUser)
    {
        jobUser.setUpdateTime(DateUtils.getNowDate());
        return jobUserMapper.insertJobUser(jobUser)>0;
    }

    @Override
    public boolean addJobUser(JobUser jobUser){
        JobUser user=jobUserMapper.getJobUserByUserId(ApiUtils.getUserId());
        if(user!=null){
            jobUser.setJobUserId(user.getJobUserId());
            return this.updateJobUser(jobUser);
        }
        jobUser.setUserId(ApiUtils.getUserId());
        return this.insertJobUser(jobUser);
    }

    /**
     * 修改求职用户
     *
     * @param jobUser 求职用户
     * @return 结果
     */
    @Override
    public boolean updateJobUser(JobUser jobUser) {
        jobUser.setUpdateTime(DateUtils.getNowDate());
        return jobUserMapper.updateJobUser(jobUser)>0;
    }

    /**
     * 批量删除求职用户
     *
     * @param jobUserIds 需要删除的求职用户ID
     * @return 结果
     */
    @Override
    public int deleteJobUserByIds(Long[] jobUserIds)
    {
        return jobUserMapper.deleteJobUserByIds(jobUserIds);
    }

    /**
     * 删除求职用户信息
     *
     * @param jobUserId 求职用户ID
     * @return 结果
     */
    @Override
    public int deleteJobUserById(Long jobUserId)
    {
        return jobUserMapper.deleteJobUserById(jobUserId);
    }
}
